package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javabean.Orders;
import com.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    OrdersService ordersService;

    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, @RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "5")  int limit
            , @RequestParam(value = "key",required = false) String key, @RequestParam(value = "value",required = false)String value){
        String orderNum="";
        String productName="";
        if("orderNum".equals(key)){
            orderNum=value;
        }else if("productName".equals(key)){
            productName=value;
        }
        System.out.println("------------------------------------------------------------------------"+orderNum+"|"+productName);
        PageHelper.startPage(page,limit);
        List<Orders> all = ordersService.findAllData(orderNum, productName);
        PageInfo<Orders> pageInfo=new PageInfo<>(all);
        request.setAttribute("pageInfo", pageInfo);
        request.setAttribute("select", key);
        request.setAttribute("selectValue", value);

        return "/orders/orders-list";
    }

    @RequestMapping("/findById")
    public String findById(HttpServletRequest request,int id){
        Orders order = ordersService.findById(id);
        request.setAttribute("order", order);
        return "/orders/orders-show";
    }
}